use "$project_path/data/4_regdata/regdata", clear

log using "$project_path/output/appendix_tables/Appendix_Table_E2.smcl", replace

// Column 1 (Minus inflation, high inflation)
rdrobust mstd_diff_m1_inflation_imf runvar if subsample_high_inflation==1, all

// Column 2 (Minus inflation, low inflation)
rdrobust mstd_diff_m1_inflation_imf runvar if subsample_low_inflation==1, all

// Column 3 (Minus unemployment, high unemployment)
rdrobust mstd_diff_m1_unemployment_ilo runvar if subsample_high_unemployment==1, all

// Column 4 (Minus unemployment, low unemployment)
rdrobust mstd_diff_m1_unemployment_ilo runvar if subsample_low_unemployment==1, all

// Column 5 (Minus inflation, high CBI)
rdrobust mstd_diff_m1_inflation_imf runvar if subsample_high_cbi==1, all

// Column 6 (Minus inflation, low CBI)
rdrobust mstd_diff_m1_inflation_imf runvar if subsample_low_cbi==1, all

log close

/*
This programs tests the equality of two coefficients using the Z-test proposed by https://psycnet.apa.org/record/1995-27766-001
*/
capture program drop equality_test
program define equality_test, rclass
	args est1 est2 se1 se2
	local z = (`est1'-`est2')/sqrt((`se1')^2 + (`se2')^2)
	local pval=2*(1-normal(abs(`z')))
	local formatted_pval : di %6.3f `pval'
    return local formatted_pval = "`formatted_pval'"
end


*** Building .tex table
// Regressions 
#delimit ;
local outcomes  
		mstd_diff_m1_inflation_imf
		mstd_diff_m1_unemployment_ilo
		mstd_diff_m1_inflation_imf
;
#delimit cr		

local subsample_count 6

// Initialize matrix of results and define value labels
local ncols = `subsample_count'*2
local nrows = 3

//Prepare matrix of coefficients and other statistics
mat Coefs = J(`nrows', `ncols', .)

mat list Coefs
	
local subsamples "_high_ _low_"


//inizialize indices
local col = 1
local pval_col = `subsample_count'+1

foreach sub in `subsamples'{
	rdrobust mstd_diff_m1_inflation_imf runvar if subsample`sub'inflation == 1, all
	
	//Store estimates and stats
	local b_`col': di %5.3f e(tau_cl)
	local se_`col': di %5.3f e(se_tau_rb)
	local p_`col': di %5.3f e(pv_rb)
	local N_`col': di %6.0f e(N)

	
	local col = `col'+2
	local pval_col = `pval_col'+2
	
	rdrobust mstd_diff_m1_unemployment_ilo runvar if subsample`sub'unemployment == 1, all	
	
	//Store estimates and stats
	local b_`col': di %5.3f e(tau_cl)
	local se_`col': di %5.3f e(se_tau_rb)
	local p_`col': di %5.3f e(pv_rb)
	local N_`col': di %6.0f e(N)
	
	local col = `col'+2
	local pval_col = `pval_col'+2
	
	rdrobust mstd_diff_m1_inflation_imf runvar if subsample`sub'cbi == 1, all	
	
	//Store estimates and stats
	local b_`col': di %5.3f e(tau_cl)
	local se_`col': di %5.3f e(se_tau_rb)
	local p_`col': di %5.3f e(pv_rb)
	local N_`col': di %6.0f e(N)
	
	local col = 2
	local pval_col = `subsample_count'+2
}

forvalues i=1/6{
	mat Coefs[1,`i'] = `b_`i''
	mat Coefs[2,`i'] = `se_`i''
	mat Coefs[3,`i'] = `N_`i''
	mat Coefs[1,`subsample_count'+`i'] = `p_`i''
}

equality_test `b_1' `b_2' `se_1' `se_2' 
local equality_i = r(formatted_pval)
equality_test `b_3' `b_4' `se_3' `se_4' 
local equality_u = r(formatted_pval)
equality_test `b_5' `b_6' `se_5' `se_6' 
local equality_cbi = r(formatted_pval)

mat list Coefs

********************************************************************************
* 								EXPORT TABLE
********************************************************************************			
	// Clear the dataset, and use the matrix as dataset			

	clear
	svmat Coefs

	//	input variable names
	input str22 varname 
	"Electoral turnover"
	"SE"
	"N"
	
	replace varname = "" if varname == "SE"
	order varname
	
	// Rename cols and labels
	forvalues n = 1/`subsample_count'{
		rename Coefs`n' Model`n'
	}
	
	//Cols where we store pvalues
	local n_min = `subsample_count' + 1
	local n_max = `subsample_count'*2
	local i = 1
	forvalues n = `n_min'/`n_max'{
		rename Coefs`n' P_Model`i'
		local ++i
	}

	label var varname ""
	label var Model1 "High inflation"
	label var Model2 "Low inflation"
	label var Model3 "High emp."
	label var Model4 "Low emp."
	label var Model5 "High CBI"
	label var Model6 "Low CBI"

	// Convert to string and add options
	forvalues i = 1/`subsample_count'{
		tostring Model`i', replace force format(%04.2f)
		replace Model`i'= "" if Model`i'=="."
		//Add parenthesis to SE and P-val
		replace Model`i' = "(" + Model`i' + ")" if missing(varname) & Model`i' !=""	
		//Remove decimals to N obs
		replace Model`i' = substr(Model`i', 1, strpos(Model`i', ".")-1) if varname == "N"

	}
	
	// Add stars
	local 3_star 0.01
	local 2_star 0.05
	local 1_star 0.10
	
	forvalues i = 1/`subsample_count'{
		
		replace Model`i' = Model`i' + "***" if P_Model`i' < `3_star'
		replace Model`i' = Model`i' + "**" if P_Model`i' < `2_star' & P_Model`i' > `3_star'
		replace Model`i' = Model`i' + "*" if P_Model`i' < `1_star' & P_Model`i' > `2_star'
		
		drop P_Model`i'
	}
	
	foreach var of varlist _all {
		qui replace `var' = ustrtrim(`var')	
	}

	local title = "Policy leverage"
	local filename = "het_policy_leverage"
	local output = "$project_path/output/appendix_tables/Appendix_Table_E2.tex"
	local headerlines = " & \multicolumn{2}{c}{\textbf{(Minus) Inflation}} & \multicolumn{2}{c}{\textbf{(Minus) Unemployment}} & \multicolumn{2}{c}{\textbf{(Minus) Inflation}}"
local footnote = "\caption*{\footnotesize \emph{Notes}: In column (1) (resp., 2), we report the estimated effect of electoral turnovers on (minus) inflation when inflation on the year before the election is above (resp., below) the median. In column (3) (resp., 4), we report the estimated effect of electoral turnovers on (minus) unemployment when unemployment on the year before the election is above (resp., below) the median. Finally, in column (5) (resp., 6), we report the estimated effect of electoral turnovers on (minus) inflation when the independence of the central bank \citep[measured by][]{garriga2016central} on the year before the election is above (resp., below) the median. In each case, we measure the median in the subsample of elections with a running variable between -15pp and +15pp. Using the method of \cite{clogg1995statistical}, we can reject the equality of the estimates of columns (1) and (2) (p-val. = `equality_i'), of the estimates of columns (3) and (4) (p-val. = `equality_u'), but not of the estimates of columns (5) and (6) (p-val. = `equality_cbi'). We obtain broadly consistent results when running a parametric regression in which we include the interaction between the treatment and the dimension of heterogeneity.}"

	texsave using "`output'", replace title("`title'") varlabels noendash hlines(2) ///
							footnote("`footnote'") ///
							headerlines("`headerlines'" "& (1) & (2) & (3) & (4) & (5) & (6) " ) ///
							frag nofix marker("tab:`filename'") location(H)
	